#define _CRT_SECURE_NO_WARNINGS 1

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    struct ListNode* phead = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
    phead->next = NULL;
    phead->val = 0;
    tmp = phead;
    while (list1 && list2)
    {
        if (list1->val <= list2->val)
        {
            tmp->next = list1;
            tmp = tmp->next;
            list1 = list1->next;
        }
        else
        {
            tmp->next = list2;
            tmp = tmp->next;
            list2 = list2->next;
        }
    }
    if (list1)
    {
        tmp->next = list1;
    }
    if (list2)
    {
        tmp->next = list2;
    }
    return phead->next;
}

ListNode* partition(ListNode* pHead, int x)
{
    struct ListNode* head1, * head2, * tail1, * tail2;
    head1 = tail1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    head2 = tail2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* cur = pHead;

    while (cur)
    {
        if (cur->val < x)
        {
            tail1->next = cur;
            tail1 = tail1->next;
        }
        else
        {
            tail2->next = cur;
            tail2 = tail2->next;
        }
        cur = cur->next;
    }
    tail1->next = head2->next;
    tail2->next = NULL;
    pHead = head1->next;
    free(head1);
    free(head2);
    return pHead;
}